/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.binders;

import android.os.RemoteException;
import android.util.Log;

import ecarx.xsf.notificationcenter.utils.DataParcelUtil;

import ecarx.eas.notification.StatusBarTyrantNotification;
import ecarx.xsf.notificationcenter.NotificationCenterData;
import ecarx.xsf.notificationcenter.common.Constants;
import ecarx.xsf.notificationcenter.manager.NotificationDispatchManager;
import ecarx.xsf.notificationcenter.scenecard.ISceneCardInfoV2;
import ecarx.xsf.notificationcenter.sdk.IEASLCService;
import ecarx.xsf.notificationcenter.sdk.IStatusBarNotification;


public class EASLCService extends IEASLCService.Stub {
    private static final String TAG = "EASLCService";
    private static final boolean DEBUG = false;

    @Override
    public int notify(IStatusBarNotification notification) throws RemoteException {
        try {
            Log.i(TAG,"notify receive a notification [" + notification.toString() + "]");
            int style = notification.getStyle();
            if (style == Constants.STYLE_TYRANT) {
                StatusBarTyrantNotification sbtn = DataParcelUtil.transform(notification);
                NotificationCenterData.getInstance().enqueueTyrantNotification(sbtn);
            } else if (style == Constants.STYLE_SMART_CARD) {
                ISceneCardInfoV2 info = DataParcelUtil.transformToCard(notification);
                if (DEBUG) {
                    Log.d(TAG, "smart card " + info.toString());
                }
                NotificationDispatchManager.get().dispatch(info);
            } else if (style == Constants.STYLE_SMART_TIP) {
                NotificationDispatchManager.get().dispatch(notification);
            }
        } catch (Exception e) {
            Log.e(TAG, "call method exception " + e);
            return Constants.RESULT_FAIL;
        }

        return Constants.RESULT_OK;
    }

    @Override
    public int cancel(IStatusBarNotification notification) throws RemoteException {
        try {
            Log.i(TAG,"cancel receive a notification [" + notification.toString() + "]");
            int style = notification.getStyle();
            if (style == Constants.STYLE_TYRANT) {
                StatusBarTyrantNotification sbtn = DataParcelUtil.transform(notification);
                NotificationCenterData.getInstance().cancelTyrantNotification(sbtn);
                if (DEBUG) {
                    Log.d(TAG, "tyrant cancel " + sbtn.toString());
                }
            } else if (style == Constants.STYLE_SMART_CARD) {
                ISceneCardInfoV2 info = DataParcelUtil.transformToCard(notification);
                if (DEBUG) {
                    Log.d(TAG, "card info cancel " + info.toString());
                }
                NotificationDispatchManager.get().removeSceneCard(info);
            } else if (style == Constants.STYLE_SMART_TIP) {
                if (DEBUG) {
                    Log.d(TAG, "tips info cancel " + notification.toString());
                }
                NotificationDispatchManager.get().cancelNotification(notification);
            }
        } catch (Exception e) {
            Log.e(TAG, "cancel method exception " + e);
            return Constants.RESULT_FAIL;
        }
        return Constants.RESULT_OK;
    }
}
